package cn.view;

import cn.comon.Common;
import cn.entity.Employee;

import javax.swing.*;
import java.awt.*;

public class UserView extends JFrame {

    Employee emp;
    JDesktopPane desk;

    public UserView(Employee emp) {
        this.emp = emp;

        // Create a custom menu bar
        CustomMenuBar menubar = new CustomMenuBar();
        this.setJMenuBar(menubar);

        // Create menus
        JMenu menu1 = new JMenu("查看信息");
        JMenuItem item1 = new JMenuItem("我的信息");
        item1.addActionListener(e -> userManage());
        menu1.add(item1);

        JMenuItem itemGoods = new JMenuItem("商品信息统计");
        itemGoods.addActionListener(e -> goodsSQView());
        menu1.add(itemGoods);

        JMenuItem itemPay = new JMenuItem("采购信息统计");
        itemPay.addActionListener(e -> paySQView());
        menu1.add(itemPay);

        // Add menus to the custom menu bar
        menubar.add(menu1);

        // Set desktop pane
        desk = Common.setDesk("picture/3.png");
        getContentPane().add(desk);
        this.setTitle("用户界面");
        this.setSize(815, 562);
        this.setLocationRelativeTo(null);
        this.setResizable(true);
        this.setVisible(true);
    }

    protected void userManage() {
        desk.add(new UserManageView());
    }

    protected void goodsSQView() {
        desk.add(new GoodsSQView());
    }

    protected void paySQView() {
        PaySQView paySQView = new PaySQView(); // 创建 PaySQView 实例
        paySQView.setVisible(true); // 设置内部窗体可见
        desk.add(paySQView); // 将内部窗体添加到 JDesktopPane 中
    }

    // Custom menu bar class
    class CustomMenuBar extends JMenuBar {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            // Calculate the total width of the menu bar
            int totalWidth = 0;
            for (int i = 0; i < getMenuCount(); i++) {
                totalWidth += getMenu(i).getPreferredSize().width;
            }

            // Calculate the starting point to center the menu bar
            int startX = (getWidth() - totalWidth) / 2;

            // Set the position of each menu
            for (int i = 0; i < getMenuCount(); i++) {
                JMenu menu = getMenu(i);
                menu.setLocation(startX, menu.getY());
                startX += menu.getPreferredSize().width;
            }
        }

        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            size.width = getParent().getWidth();
            return size;
        }
    }

}
